package com.dlgcy.xinlang_weibo;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import weibo4j.Timeline;
import weibo4j.http.ImageItem;
import weibo4j.model.Status;

import com.dlgcy.weibo.ConfigUtil;

public class Forward_xl {
	public static void upload(String access_token, String con, String pic_path) {
		try {
			try {
				byte[] content = readFileImage(pic_path);
				System.out.println("content length:" + content.length);
				ImageItem pic = new ImageItem("pic", content);
				String s = java.net.URLEncoder.encode(con, "utf-8");
				Timeline tl = new Timeline();
				tl.client.setToken(access_token);// access_token
				Status status = tl.UploadStatus(s, pic);

				Thread.sleep(ConfigUtil.getIntValue("nSecond") * 1000); // 减缓发送；

				// System.out.println("Successfully upload the status to [" +
				// status.getText() + "].");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception ioe) {
			System.out.println("Failed to read the system input.");
		}
	}

	public static byte[] readFileImage(String filename) throws IOException {
		BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filename));
		int len = bufferedInputStream.available();
		byte[] bytes = new byte[len];
		int r = bufferedInputStream.read(bytes);
		if (len != r) {
			bytes = null;
			throw new IOException("读取文件不正确");
		}
		bufferedInputStream.close();
		return bytes;
	}
}
